package jzqb.dao.impl;

import cnki.tpi.base.DataContext;
import cnki.tpi.base.DataSourceManager;
import cnki.tpi.kbatis.sqlsession.SqlSession;
import cnki.tpi.kbatis.utils.DataSourceUtil;
import haha.life.common.BelongToSqlUtils;
import haha.life.common.SysTool;
import haha.life.normal.FileUtil;
import haha.life.normal.FileuploadUtils;
import jzqb.bo.AdminQueryBo;
import jzqb.dao.AdminZTJDao;
import jzqb.po.CGPo;
import jzqb.po.ZTJPo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 专题集数据层实现类
 */
@Repository
public class AdminZTJDaoImpl implements AdminZTJDao {
    @Autowired
    private DataContext dataContext;

    @Autowired
    private DataSourceManager kbaseDs;

    @Value("${filePath.coverTemPResource}")
    private String coverTemPResource;

    @Value("${filePath.ztjCoverPath}")
    private String ztjCoverPath;
    @Override
    public Boolean updateZTJ(ZTJPo ztjPo) {
        boolean fileRes = true;
        String cover = ztjPo.getCover();
        String newCover = ztjPo.getNewCover();
        Integer sysID = ztjPo.getSysID();
        String dbAbsPath,tempPath,before= "";
        // 1. 把新封面从临时目录移动到数据库，并且删除原封面
        if (!"".equals(newCover) && newCover != null){
            if(!"".equals(cover) && cover != null){
                before = FileUtil.calculateAbstractPath(cover,ztjCoverPath);
                FileuploadUtils.deleteFileFromDBFile(this.kbaseDs, before.substring(0,before.lastIndexOf("\\")));
            }
            dbAbsPath = FileUtil.calculateAbstractPath(newCover,ztjCoverPath);
            tempPath = FileUtil.calculateAbstractPath(newCover,coverTemPResource);
            try {
                fileRes = FileuploadUtils.moveTempFileToDBFile(this.kbaseDs,tempPath,dbAbsPath);
            } catch (Exception e) {
                e.printStackTrace();
            }
            ztjPo.setCover(newCover);
        }

        //2. 更新
        String recStr = null;
        recStr = FileuploadUtils.generRECfile("fieldsRemap_ztj.json","edit", cover,sysID,ztjPo);
        String tableName = SysTool.ZTJ_METADATA;
        boolean updateRes = dataContext.updateRecData("SYS_FLD_SYSID", tableName, recStr);
        return updateRes && fileRes;
    }

    @Override
    public Boolean insertZTJ(ZTJPo po) {
        Integer sysID = po.getSysID();
        String cover = po.getCover();
        String dbAbsPath,tempPath = "";
        boolean fileRes = true;
        //1 把封面从 临时目录 移动到 数据库
        if (!"".equals(cover)){
            dbAbsPath = FileUtil.calculateAbstractPath(cover,ztjCoverPath);
            tempPath = FileUtil.calculateAbstractPath(cover,coverTemPResource);
            try {
                fileRes = FileuploadUtils.moveTempFileToDBFile(this.kbaseDs,tempPath,dbAbsPath);
//                fileRes = FileuploadUtils.fileMove(tempPath,dbAbsPath);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        //3 写入数据到数据库
        String recStr = FileuploadUtils.generRECfile("fieldsRemap_ztj.json","add",cover, sysID,po);
        String tableName = SysTool.ZTJ_METADATA;
        boolean insetRes = dataContext.insertRecData(tableName,recStr);
        return insetRes && fileRes;
    }

    @Override
    public Boolean deleteZTJ(ZTJPo ztjPo) {
        //删除数据库数据
        SqlSession sqlSession = DataSourceUtil.getSqlSession();
        String statementId = "jzqb.dao.AdminZTJDao.deleteZTJ";
        return sqlSession.executeDelete(statementId, ztjPo.getSysID());
    }

    @Override
    public List<ZTJPo> queryZTJ(AdminQueryBo bo) {
        SqlSession sqlSession = DataSourceUtil.getSqlSession();
        String statementId = "jzqb.dao.AdminZTJDao.queryZTJ";
        return sqlSession.selectList(statementId, bo, false);
    }

    @Override
    public Long count(AdminQueryBo bo) {
        SqlSession sqlSession = DataSourceUtil.getSqlSession();
        String statementId = "jzqb.dao.AdminZTJDao.count";
        return sqlSession.getCount(statementId, bo);
    }

    @Override
    public ZTJPo queryZTJByName(AdminQueryBo bo) {
        SqlSession sqlSession = DataSourceUtil.getSqlSession();
        String statementId = "jzqb.dao.AdminZTJDao.queryZTJByName";
        return sqlSession.selectOne(statementId,bo,false);
    }

    @Override
    public List<ZTJPo> queryZTJById(String ztjId) {
        String[] ztjArr= ztjId.split(",");
        String ztjSql = BelongToSqlUtils.getBelongToSql(ztjArr, "ID");
        SqlSession sqlSession = DataSourceUtil.getSqlSession();
        String statementId = "jzqb.dao.AdminZTJDao.queryZTJById";
        return sqlSession.selectList(statementId, ztjSql, false);
    }


    @Override
    public List<ZTJPo> queryNameAndId(ZTJPo ztjPo) {
        SqlSession sqlSession = DataSourceUtil.getSqlSession();
        String statementId = "jzqb.dao.AdminZTJDao.queryNameAndId";
        return sqlSession.selectList(statementId, ztjPo, false);
    }




}
